import * as echarts from '../../plugin/ec-canvas/echarts';
// pages/login/login.js
//获取应用实例
const app = getApp()
const util = require('../../../utils/util.js')
var chart1 = null
var chart2 = null
var chart3 = null
var chart4 = null
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab:0,
    inoutrecordData:[],
    inoutrecordList:[],
    inoutrecordLoadingData:[],
    changeSearch: 0, //改变搜索框状态
    loading: 0,
    flag: 0 ,
    searchInput:"",
    curTime:'xxxx-xx-xx xx:xx:xx',
    noMore: true,
    ec1:{
      onInit: function (canvas, width, height) {
        chart1 = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(chart1);
        return chart1;
      }
     },
     ec2:{
      onInit: function (canvas, width, height) {
        chart2 = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(chart2);
        return chart2;
      }
     },
     ec3:{
      onInit: function (canvas, width, height) {
        chart3 = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(chart3);
        return chart3;
      }
     },
     ec4:{
      onInit: function (canvas, width, height) {
        chart4 = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(chart4);
        return chart4;
      }
     }
  },
  onLoad: function(options) {
    wx.request({
      url: app.baseUrl +"/WeChat/schoolMangement/getSchoolUserTypeCount",
      method: 'POST',
      header:{
        "content-type":"application/json"
      },
      data: JSON.stringify({
        department: app.globalData.departmentId
      }),
      dataType: 'json',
      success: res => {
        wx.setStorageSync('total',res.data)
      }
    })
    this.setData({
       schoolName : app.globalData.department
    })
  },

  changeSearch: util.throttle(function() {
    this.setData({
      changeSelect: this.data.changeSelect == 0 ? 0 : this.data.changeSelect == 1 ? 2 : 2,
      changeSearch: this.data.changeSearch == 0 ? 1 : this.data.changeSearch == 1 ? 2 : 1
    })
  }, 500),

  //从搜索按钮 点击搜索
  searchButton: function(e) {
    this.setData({
      loading: 1
    })
    let inputContent = app.trim(e.detail.value.input)
    this.searchItem(inputContent)
  },
  //从键盘确定 点击搜索
  searchKeyboard: function(e) {
    let input = app.trim(e.detail.value)
    this.searchItem(input)
  },
  searchItem: function(inputContent) {
    if (!inputContent) {
      wx.showToast({
        title: '请输入关键字！',
        icon: 'none',
        duration: 800,
        mask: true
      })
      this.setData({
        loading: 0
      })
    }else{
      let inoutrecordData = []
      let dateTime
      if(this.data.searchInput == ""){
        dateTime = util.formatTime2(new Date());
      }else{
        dateTime = this.data.searchInput
      }
      wx.request({
        url: app.baseUrl + '/WeChat/schoolMangement/getFaceRecord',
        method: 'POST',
        header:{
          "content-type":"application/json"
        },
        data: JSON.stringify({
          department: app.globalData.departmentId,
          timeAfter: dateTime,
          timePre: dateTime,
        }),
        dataType: 'json',
        success: res => {
          res.data.map((item,index)=>{
              var time =  item.time.split("-");
              var time2 = time[1]+"-"+time[2] ;
              if(item.userType === 1){
                if(item.enter === 'in'){
                  inoutrecordData.push({
                    username:item.name,
                    userType:'教师',
                    inoutStatus:'入校',
                    inoutTime:time2,
                    grade:''
                  })
                }else if(item.enter === 'out'){
                  inoutrecordData.push({
                    username:item.name,
                    userType:'教师',
                    inoutStatus:'离校',
                    inoutTime:time2,
                    grade:''
                  })
                }
              }else if(item.userType === 2){
                if(item.enter === 'in'){
                  inoutrecordData.push({
                    username:item.name,
                    userType:'后勤人员',
                    inoutStatus:'入校',
                    inoutTime:time2,
                    grade:''
                  })
                }else if(item.enter === 'out'){
                  inoutrecordData.push({
                    username:item.name,
                    userType:'后勤人员',
                    inoutStatus:'离校',
                    inoutTime:time2,
                    grade:''
                  })
                }
              }else if(item.userType === 3){
                if(item.enter === 'in'){
                  inoutrecordData.push({
                    username:item.name,
                    userType:'学生',
                    inoutStatus:'入校',
                    inoutTime:time2,
                    grade:item.grade
                  })
                }else if(item.enter === 'out'){
                  inoutrecordData.push({
                    username:item.name,
                    userType:'学生',
                    inoutStatus:'离校',
                    inoutTime:time2,
                    grade:item.grade
                  })
                }
              }else if(item.userType === 4){
                if(item.enter === 'in'){
                  inoutrecordData.push({
                    username:item.name,
                    userType:'家属',
                    inoutStatus:'入校',
                    inoutTime:time2,
                    grade:''
                  })
                }else if(item.enter === 'out'){
                  inoutrecordData.push({
                    username:item.name,
                    userType:'家属',
                    inoutStatus:'离校',
                    inoutTime:time2,
                    grade:''
                  })
                }
              }
          })
          let inoutrecordList = []
          if(this.data.dateFlag == 0){
            inoutrecordList = inoutrecordData.filter(p => {
              return p.username.includes(inputContent)  || p.userType.includes(inputContent) ||
               p.inoutStatus.includes(inputContent) || p.inoutTime.includes(inputContent) ||
               p.grade.includes(inputContent)
            })
          }else{
            inoutrecordList = inoutrecordData
          }
          if (inoutrecordList.length == 0) {
            wx.showToast({
              title: '未搜索到符合条件的项目，请重新输入！',
              icon: 'none',
              duration: 800,
              mask: true
            })
            this.setData({
              loading: 0
            })
            return
          }
          wx.showToast({
            title: '搜索成功！',
            icon: 'none',
            duration: 1000
          })
          this.setData({
            inoutrecordList:inoutrecordList,
            loading: 0
          })
        }
      })
      }
  },
/**
   * 点击tab切换
   */
  clearSelect:function(){
    wx.showToast({
      title: '清除成功',
      icon: 'none',
      duration: 800,
      mask: true
    })
    this.setData({
      inoutrecordList : [],
      searchInput: ""
    })
  },
  /**
   * 点击tab切换
   */
  swichNav:function(e){
    this.setData({
      list: [],
      currentTab: e.currentTarget.dataset.current
    })
    if (this.data.currentTab == 0) {
      
    } else if (this.data.currentTab == 1) {
       
    } 
  },
    /**
   * 滑动切换tab
   */
  bindChange: util.throttle(function (e) {
    console.log(e)
    this.setData({
      currentTab: e.detail.current
    })
    if (this.data.currentTab == 0) {
      
    } else if (this.data.currentTab == 1) {
      let total = wx.getStorageSync('total')
      let studentTotal =  total.studentTotal;
      let otherTotal =  total.otherTotal ;
      let curTime  = util.formatTime(new Date())
      this.setData({
        studentTotal : studentTotal,
        otherTotal: otherTotal,
        curTime: curTime
      })
      let dateTime=util.formatTime2(new Date());
      let startTime = dateTime + ' 00:00:00'
      console.log('startTime',startTime)
      chart1.showLoading();
      chart2.showLoading();
      chart3.showLoading();
      wx.request({
        url: app.baseUrl +'/WeChat/schoolMangement/getStudentAndOtherRoleInOutRecord',
        method: 'POST',
        header:{
          "content-type":"application/json"
        },
        data: JSON.stringify({
          department: app.globalData.departmentId,
          timeAfter: curTime,
          timePre: startTime,
        }),
        dataType: 'json',
        success: res => {
            let studentInTimesList = [] 
            let studentOutTimesList = [] 
            res.data.studentInOutList.forEach((item,index)=>{
              if(item.grades == ''){
                studentInTimesList.push({
                  value : item.inTimes ,
                  name : '暂无学生班级'
               })
               studentOutTimesList.push({
                 value : item.outTimes ,
                 name : '暂无学生班级'
               })
              }else{
                studentInTimesList.push({
                  value : item.inTimes ,
                  name : item.grades
               })
               studentOutTimesList.push({
                value : item.outTimes ,
                name : item.grades
               })
              }
            })
            let otherRoleInTimes =  res.data.teacherInOutList[0].inTimes 
            let otherRoleOutTimes = res.data.teacherInOutList[0].outTimes 
            //当前学生分年级到校考勤统计',
           chart1.setOption({
            tooltip: {
              trigger: 'item',
              formatter: '{b}: {c} ({d}%)'
            },
            title: {
              left: 'center',
              top:'10px'
            },
            series: [
               {
                  type: 'pie',
                  radius: ['30%', '45%'],
                  center: ['50%', '50%'] ,
                  label: {
                      formatter: '{b|{b}：}{c|{c}}',
                      backgroundColor: '#eee',
                      borderColor: '#aaa',
                      rich: {
                          b: {
                              fontSize: 18,
                          },
                          c:{
                              fontSize: 18,
                          },
                          per: {
                              fontSize: 18,
                              color: '#eee',
                              backgroundColor: '#334455',
                              padding: [4, 2],
                              borderRadius: 2
                          }
                      }
                  },
                  data: studentInTimesList
               }
            ]
           })
           chart1.hideLoading();
           //当前学生分年级离校考勤统计',
           chart2.setOption({
            tooltip: {
              trigger: 'item',
              formatter: '{b}: {c} ({d}%)'
            },
            title: {
              left: 'center',
              top:'10px'
            },
            series: [
               {
                  type: 'pie',
                  radius: ['30%', '45%'],
                  center: ['50%', '50%'] ,
                  label: {
                      formatter: '{b|{b}：}{c|{c}}',
                      backgroundColor: '#eee',
                      borderColor: '#aaa',
                      rich: {
                          b: {
                              fontSize: 18,
                          },
                          c:{
                              fontSize: 18,
                          },
                          per: {
                              fontSize: 18,
                              color: '#eee',
                              backgroundColor: '#334455',
                              padding: [4, 2],
                              borderRadius: 2
                          }
                      }
                  },
                  data: studentOutTimesList
               }
            ]
           })
           chart2.hideLoading();
           //当前教工到校/离校考勤统计',
           chart3.setOption({
            tooltip: {
              trigger: 'item',
              formatter: '{b}: {c} ({d}%)'
            },
            title: {
              left: 'center',
              top:'10px'
            },
            series: [
               {
                  type: 'pie',
                  radius: ['30%', '45%'],
                  center: ['50%', '50%'] ,
                  label: {
                      formatter: '{b|{b}：}{c|{c}}',
                      backgroundColor: '#eee',
                      borderColor: '#aaa',
                      rich: {
                          b: {
                              fontSize: 18,
                          },
                          c:{
                              fontSize: 18,
                          },
                          per: {
                              fontSize: 18,
                              color: '#eee',
                              backgroundColor: '#334455',
                              padding: [4, 2],
                              borderRadius: 2
                          }
                      }
                  },
                  data: [
                      {value: otherRoleInTimes , name: '到校'},
                      {value: otherRoleOutTimes , name: '离校'},
                  ]
               }
            ]
           })
           chart3.hideLoading();

        }
      })
    } 
  }, 150),
  toInDetail:function(){
     wx.navigateTo({
       url: '../inoutrecordDetail/inoutrecordDetail?enter=in',
     })
  },
  toOutDetail:function(){
    wx.navigateTo({
      url: '../inoutrecordDetail/inoutrecordDetail?enter=out',
    })
  },
  //日期选择器
  bindDateChange:function(e){
    this.setData({
      searchInput : e.detail.value,
      dateFlag : 1 
    })
  },
  getInputStr:function(e){
    this.setData({
      dateFlag : 0
    })
  },

})

